#include "stdafx.h"
#include "DDXray.h"
#include "PanImage.h"
#include "DlgTool.h"
#include "DD_ImgCfg.h"
#include "DlgSysProductInfo.h"
#include "DlgParseImageResult.h"
#include "Public.h"
#include "DlgMsgBox.h"


void CDlgTool::OnBUTTONTeach()
{
	OnBnClickedButtonHide();
	g.DlgMainFace->PostMessage(WM_COMMAND,IDC_BUTTON_Teach);
}



void CDlgTool::ShowEdgeStatistics(void* pStatistics)
{
	zkAnalyEdgeHead *pHead = (zkAnalyEdgeHead*)pStatistics;

    SetDlgItemInt( IDC_EDIT_EddeCountTimes,pHead->dwRecCount );
	SetDlgItemInt( IDC_EDIT_EdgeThreshold,(int)pHead->fEdgeThreshold );
}

int CDlgTool::OnRefHistogramFun(int nLeft,int nRight,int nGrayLevels,DWORD dwContext)
{
	int nTemp[4];
	nTemp[0] = nLeft;
	nTemp[1] = nRight;
	nTemp[2] = nGrayLevels;

	s.ImgAnaly.ImageCtrl(eAnalyWindow_Ref,ImageCtrl_Histogram,nTemp);

	g.ViewmAnaly[eAnalyWindow_Ref]->DDOnDraw();

	return 0;
}



int CDlgTool::OnSubHistogramFun(int nLeft,int nRight,int nGrayLevels,DWORD dwContext)
{
	int nTemp[4];
	nTemp[0] = nLeft;
	nTemp[1] = nRight;
	nTemp[2] = nGrayLevels;

	s.ImgAnaly.ImageCtrl(eAnalyWindow_Sub,ImageCtrl_Histogram,nTemp);

	g.ViewmAnaly[eAnalyWindow_Sub]->DDOnDraw();

	SetDlgItemInt(IDC_EDIT_CheckSubHisN1,nLeft);
	SetDlgItemInt(IDC_EDIT_CheckSubHisN2,nRight);

	return 0;
}
void CDlgTool::SubImgHistogram()
{
	zkImgHistogram *pVoid = NULL;
	int nRet = s.ImgAnaly.GetHistogramInfo(eAnalyWindow_Sub,&pVoid);
	if( DD_ERR_SUCCESS == nRet && NULL != pVoid )
	{
		m_pSubImgHis->SetHistogramInfo((zkImgHistogram *)pVoid);
        m_pSubImgHis->SetGrayRange( pVoid->nDark,pVoid->nBright );
		SetDlgItemInt(IDC_EDIT_CheckSubHisN1,pVoid->nDark);
		SetDlgItemInt(IDC_EDIT_CheckSubHisN2,pVoid->nBright);
	} 

	ShowAdjEdgeImgBinaryTool(FALSE); 
}
void CDlgTool::OnBUTTONSetCheckSubHis()
{
	int nTemp[6];
	nTemp[0] = (int)GetDlgItemInt(IDC_EDIT_CheckSubHisN1);
	nTemp[1] = (int)GetDlgItemInt(IDC_EDIT_CheckSubHisN2);
	s.ImgAnaly.ImageCtrl(eAnalyWindow_Sub,ImageCtrl_Histogram,nTemp);

	g.ViewmAnaly[eAnalyWindow_Sub]->DDOnDraw();

	if( NULL != m_pSubImgHis)
	{
		m_pSubImgHis->SetGrayRange( nTemp[0],nTemp[1] );
	}
}



void CDlgTool::ShowAdjEdgeImgBinaryTool(BOOL bShow)
{
	GetDlgItem(IDC_EDIT_AdjEdgeImgBinary)->ShowWindow(bShow);
	GetDlgItem(IDC_BTN_AdjImgBinary)->ShowWindow(bShow);
	GetDlgItem(IDC_EDIT_CheckSubHisN1)->ShowWindow(!bShow);
	GetDlgItem(IDC_EDIT_CheckSubHisN2)->ShowWindow(!bShow);
	GetDlgItem(IDC_BTN_SetCheckSubHis)->ShowWindow(!bShow);


	if( NULL != m_pAdjEdgeBinHis)
	{
		m_pAdjEdgeBinHis->ShowWindow(bShow);
	}
	if( NULL != m_pSubImgHis )
	{
		m_pSubImgHis->ShowWindow( !bShow );
	}
}
void CDlgTool::AdjEdgeImgBinHistogram()
{
	zkImgHistogram *pVoid = NULL;
	int nRet = s.ImgAnaly.GetHistogramInfo(eAnalyWindow_Sub_AdjEdge,&pVoid);
	if( DD_ERR_SUCCESS == nRet && NULL != pVoid )
	{
		m_pAdjEdgeBinHis->SetHistogramInfo((zkImgHistogram *)pVoid);
		m_pAdjEdgeBinHis->SetImgBinary( pVoid->nDark );
		SetDlgItemInt(IDC_EDIT_AdjEdgeImgBinary,pVoid->nDark); 
	} 
 
	ShowAdjEdgeImgBinaryTool(TRUE); 
}
void CDlgTool::OnBUTTONSetAdjImgBinary()
{
	int nTemp[6];
	nTemp[0] = (int)GetDlgItemInt(IDC_EDIT_AdjEdgeImgBinary);
	//s.ImgAnaly.ImageCtrl(eAnalyWindow_Sub,ImageCtrl_ImgBinary,nTemp);
	s.ImgAnaly.ImageCtrl(eAnalyWindow_Sub_AdjEdge,ImageCtrl_Histogram,nTemp);

	if( NULL != m_pAdjEdgeBinHis)
	{
		m_pAdjEdgeBinHis->SetImgBinary( nTemp[0] );
	}
}
int CDlgTool::OnAdjBinHistogramFun(int nBinary,int nGrayLevels,DWORD dwContext)
{
	int nTemp[6];
	nTemp[0] = nBinary;
	nTemp[1] = nGrayLevels;

	s.ImgAnaly.ImageCtrl(eAnalyWindow_Sub_AdjEdge,ImageCtrl_Histogram,nTemp);
	g.ViewmAnaly[eAnalyWindow_Sub]->DDOnDraw();

	SetDlgItemInt(IDC_EDIT_AdjEdgeImgBinary,nBinary);

	return 0;
}
int CDlgTool::CB_OnAdjBinHistogramFun(int nBinary,int nGrayLevels,DWORD dwContext,void *pContext)
{
	CDlgTool *pCap = (CDlgTool*)pContext;
	if( NULL != pCap )
	{
		pCap->OnAdjBinHistogramFun(nBinary,nGrayLevels,dwContext);
	}
	return 0;
}



int CDlgTool::OnSubHistogramFun(int n1,int n2,int n3,int n4,int nGrayLevels,DWORD dwContext)
{
	int nTemp[6];
	nTemp[0] = n1;
	nTemp[1] = n2;
	nTemp[2] = n3;
	nTemp[3] = n4;
	nTemp[4] = nGrayLevels;

	s.ImgAnaly.ImageCtrl(eAnalyWindow_Sub,ImageCtrl_Histogram,nTemp);

	g.ViewmAnaly[eAnalyWindow_Sub]->DDOnDraw();

	return 0;
}
int CDlgTool::CB_OnSubHistogramFun(int n1,int n2,int n3,int n4,int nMax,DWORD dwContext,void *pContext)
{
	CDlgTool *pCap = (CDlgTool*)pContext;
	if( NULL != pCap )
	{
		pCap->OnSubHistogramFun(n1,n2,n3,n4,nMax,dwContext);
	}
	return 0;
}



void CDlgTool::OnBUTTONDelDefectS3()
{
	( (CViewSub*)(g.ViewmAnaly[eAnalyWindow_Sub]) )->m_nDelDefectS  = 3;
	( (CViewSub*)(g.ViewmAnaly[eAnalyWindow_Sub]) )->m_bClearDefect = TRUE;
}
void CDlgTool::OnBUTTONDelDefectS7()
{
	( (CViewSub*)(g.ViewmAnaly[eAnalyWindow_Sub]) )->m_nDelDefectS = 7;
	( (CViewSub*)(g.ViewmAnaly[eAnalyWindow_Sub]) )->m_bClearDefect = TRUE;
}
void CDlgTool::OnBUTTONDelDefectS11()
{
	( (CViewSub*)(g.ViewmAnaly[eAnalyWindow_Sub]) )->m_nDelDefectS = 11;
	( (CViewSub*)(g.ViewmAnaly[eAnalyWindow_Sub]) )->m_bClearDefect = TRUE;
}
void CDlgTool::OnBUTTONDelDefectRestore()
{
	s.ImgAnaly.OperateMethod(eAnalyMethod_RestorSubDefect);
	( (CViewSub*)(g.ViewmAnaly[eAnalyWindow_Sub]) )->m_bClearDefect = FALSE;
}

void CDlgTool::OnBUTTONEdgeConditionThreshold()
{
	char sText[20]={0};
	GetDlgItemText(IDC_EDIT_EdgeThreshold,sText,20);
	float fTemp[6];
	fTemp[0] = atof(sText);
	s.ImgAnaly.OperateMethod(eAnalyMethod_EdgeConditionThreshold,fTemp);
}


void CDlgTool::OnBUTTONAddAdjEdgeToRef()
{
	int nTemp[6];
	nTemp[0] = (int)GetDlgItemInt(IDC_EDIT_AdjEdgeMidValue);
	s.ImgAnaly.OperateMethod(eAnalyMethod_AddAdjEdgeToRef,nTemp);
}
void CDlgTool::OnBUTTONSubImage()
{
	s.ImgAnaly.OperateMethod(eAnalyMethod_Sub);
}
void CDlgTool::OnBUTTONAddSubEdgeToRef()
{
	int nTemp[6];
	m_pSubImgHis->GetGrayRange(nTemp[0],nTemp[1]);
	s.ImgAnaly.OperateMethod(eAnalyMethod_AddSubEdgeToRef,nTemp);
}


void CDlgTool::OnBUTTONDelEdge()
{
	s.ImgAnaly.OperateMethod(eAnalyMethod_DelEdge);
}


void CDlgTool::OnBUTTONAddSapToAidRef()
{
	s.ImgAnaly.OperateMethod(eAnalyMethod_AddSapToAidRef);
}

void CDlgTool::OnCheckInitialize()
{
	GetDlgItem(IDC_BUTTON_BeginCap)->EnableWindow(TRUE);
	GetDlgItem(IDC_BUTTON_DelEdge)->EnableWindow(FALSE);
	GetDlgItem(IDC_BUTTON_ParseImage)->EnableWindow(FALSE);
	GetDlgItem(IDC_BUTTON_AddSubEdgeToRef)->EnableWindow(FALSE);
	GetDlgItem(IDC_BUTTON_AddSapToAidRef)->EnableWindow(FALSE);

	ShowAdjEdgeImgBinaryTool(TRUE); 
}

void CDlgTool::OnCheckOperateMethod(DWORD dwMsgType,int nRet)
{
	switch( dwMsgType )
	{
	case AnalyMsg_AdjImage:
		{
			GetDlgItem(IDC_BUTTON_AddAdjEdgeToRef)->EnableWindow(TRUE);
		}
		break;
	case AnalyMsg_SubImage:
		{
			GetDlgItem(IDC_BUTTON_AddAdjEdgeToRef)->EnableWindow(FALSE);
			GetDlgItem(IDC_BUTTON_DelEdge)->EnableWindow(FALSE);
			GetDlgItem(IDC_BUTTON_ParseImage)->EnableWindow(FALSE);
			GetDlgItem(IDC_BUTTON_AddSubEdgeToRef)->EnableWindow(FALSE);
			if( nRet >= AnalyMsgState_Finish )
			{
				GetDlgItem(IDC_BUTTON_DelEdge)->EnableWindow(TRUE);
				GetDlgItem(IDC_BUTTON_AddSubEdgeToRef)->EnableWindow(TRUE);
			}
		}
		break;
	case AnalyMsg_DelEdge:
		{
			if( nRet >= AnalyMsgState_Finish )
			{
				GetDlgItem(IDC_BUTTON_ParseImage)->EnableWindow(TRUE);
			}
		}
		break;
	case AnalyMsg_ParseImage:
		{
		}
		break;
	case AnalyMsg_AddSapToAidRef:
		{
		}
		break;
	default:
		break;
	}
}


#include "math.h"
#define DKQ_PI   3.141593f

void CDlgTool::ShowAdjustParamters(void *pVoid)
{
	float *pfThM = (float *) pVoid ;

	int x = (int)pfThM[2];
	int y = (int)pfThM[5];

	//m[0] = (float) (factor * cos (angle * CV_PI / 180.));
	//m[1] = (float) (factor * sin (angle * CV_PI / 180.));

	double a = asin( (double)pfThM[1] ); 

	char sText[128] = {0};
	sprintf(sText,"%g",pfThM[2]);
	SetDlgItemText(IDC_EDIT_Horizontal,sText);
	sprintf(sText,"%g",sText,pfThM[5]);
	SetDlgItemText(IDC_EDIT_Vertical,sText);
	sprintf(sText,"%g ",sText,a*180/DKQ_PI);
	SetDlgItemText(IDC_EDIT_Angle,sText);
}


void CDlgTool::OnBUTTONParseImage()
{
	s.ImgAnaly.OperateMethod(eAnalyMethod_Parse);
}

void CDlgTool::ShowResult(zkAnalyResultConfig* pCfg)
{
	CDlgParseImageResult dlg;
	dlg.m_pAnalyResult = pCfg;
	dlg.DoModal();
}



